﻿using System;
using System.Threading;
using System.Windows.Forms;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Utilities.DataConfiguration.Windows;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Utilities.DataConfiguration
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleCurrentDomainUnhandledException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += new ThreadExceptionEventHandler(HandleCurrentThreadUnhandleException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new StartupWindow());
        }

        #region HandleCurrentThreadUnhandleException
        /// <summary>
        /// 处理当前线程中未处理的异常。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void HandleCurrentThreadUnhandleException(object sender, ThreadExceptionEventArgs e)
        {
            using (ErrorDialogWindow frmErrorDialog = new ErrorDialogWindow())
            {
                frmErrorDialog.Show(e.Exception);
            }
        }
        #endregion

        #region HandleCurrentDomainUnhandledException
        /// <summary>
        /// 用于处理当前应用程序域的未处理异常。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void HandleCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            using (ErrorDialogWindow frmErrorDialog = new ErrorDialogWindow())
            {
                if (e.ExceptionObject.Is<Exception>())
                    frmErrorDialog.Show(e.ExceptionObject.ForceTo<Exception>());
            }
        }
        #endregion
    }
}
